package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.utils.UploadUtil;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private INewsService newsService;
@Autowired
private IUserService userService;
    //资讯列表
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") NewsQueryObject qo, Model model) {
        qo.setState(0);
        PageInfo<News> query = newsService.query(qo);
        model.addAttribute("pageInfo", query);
        return "news/list";
    }



//取消推荐
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(News news){
        JSONResult result = new JSONResult();
        try {
            //将状态设为普通状态
             news.setState(0);

            //更改状态
            newsService.updateState(news);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        return newsService.getNewsContentById(id);
    }
//编辑或者更新资讯
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(News news, MultipartFile multipartFile) {
        JSONResult result = new JSONResult();
        try{
//            //判断是否传入新的图片
//            if(multipartFile!=null&&multipartFile.getSize()>0){
//                String imagePath = UploadUtil.upload(multipartFile, UploadUtil.IMG_BASE_PATH + "/upload");
//                news.setCoverUrl(imagePath);
//            }
            newsService.saveOrUpdate(news);

        }catch(Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

}
